cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _Brandwall:null,
        index:0,
        zhuangSide:null,
        paiDuPos:0,
        gangNum:0,
        sprite:null,
        zhuang:null,
        isgang:false,
        gangBrandwall:null,
        animation:null,
        pre:null,
        paiqiang:false,
        // holdSprite:null,
    },

    // use this for initialization
    onLoad: function () {
        this.paiqiang = false;
        this.hidepaiqiang();
        this.initEventHandler();
        this.animation = cc.find("Canvas/mjAnimation").getComponent(cc.Animation);
        this.animation.node.active = false;
        if(cc.vv.gameNetMgr.isGameSync)
        {
            this.initView();
            this.initpaiqiang();
            if(cc.vv.gameNetMgr.paiDuPos != 0)
            {
                this.initcaishen();
                this.initBrandwalls();
            }
            // for(var i = 0;i < cc.vv.gameNetMgr.gangNum;i++)
            // {
            //     console.log("断线重连........杠"+cc.vv.gameNetMgr.gangNum);
            //     this.initGang();
            // }
        }
    },
    
    initEventHandler:function(){
        var self = this;
        this.node.on('game_begin',function(data){
            console.log("game_begin of folds");
            self.initView();
            
            self.initcaishen(data);
            self.initBrandwalls();
            self.zhuang = cc.vv.gameNetMgr.button;
        });

        // this.node.on('game_sync',function(){
        //     console.log("game_sync of brandwall.js");
        //     self.initBrandwalls();
        // });
        
        this.node.on('mopai_push',function(data){
            console.log("mo_pai of folds");
            self.initBrandwalls();
            // if(self.isgang)
            // {
            //     self.initGang();
            // }
            // else
            // {
            //     self.initmopai();
            // }
            // self.isgang = false;
        });

        this.node.on('game_mopai',function(data){
            console.log("mo_pai of folds");
            self.initBrandwalls();
            // if(self.isgang)
            // {
            //     self.initGang();
            // }
            // else
            // {
            //     self.initmopai();
            // }
            // self.isgang = false;
        });

        // this.node.on('is_mai_or_ding_di',function(data){

        // });

        this.node.on('refresh_sync_infs',function(data){
            console.log("refresh_sync_infs");
            //主要是用于停留在买/顶的选择页面中。
            if(cc.vv.gameNetMgr.gamestate='idle') {
                var type=cc.vv.gameNetMgr.maiOrDingDiArr[cc.vv.gameNetMgr.seatIndex];
                if(type==1||type==2||type==3||type==4) {
                    self.initpaiqiang();
                }
                return ;
            }
            //如果是playing状态
            //先全部点亮,再去刷新牌墙。
            self.initpaiqiang();
            self.initBrandwalls();
        });
        
        this.node.on('game_over',function(data){
            console.log("game_over of folds");
            self.paiqiang = false;
        });
        
        this.node.on('initpaiqiang',function(data){
            console.log("initpaiqiang of folds");
            self.hidepaiqiang();
            self.animation.node.active = true;
            self.playAnims();
            // self.initpaiqiang();
        });
        this.node.on('paidun_push',function(data){
            console.log("paidu_push of folds");
            console.log(data);
            self.initView();   
        });
        this.node.on('initBrand',function(data){
            console.log("initBrand of folds");
            self.initView();
            self.initcaishen();
            self.initBrandwalls();
        });
        this.node.on('gang_notify',function(data){
            console.log("gang_notify");
            self.isgang = true;
        });
    },

    playAnims:function(data)
    {
        var self = this;
        this.animation.play("mjanimation");
        this.animation.on('finished',self.initpaiqiang,this);
    },

    initView:function(){
        this._Brandwall = [];
        var game = this.node.getChildByName("game");
        var localIndex =cc.vv.gameNetMgr.getLocalIndex(cc.vv.gameNetMgr.button);
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        this.zhuangSide = side;
        var sides = ["myself","right","up","left"];
        var number = 0;
         switch (side) {
                case "myself":
                    number = 0;
                    break;
                case "right":
                    number = 1;
                    if(cc.vv.setPeople.isERMJ())
                    {
                        number = 2;
                    }
                    break;
                case "up":
                    number = 2;
                    break;
                case "left":
                    number = 3;
                    break;
             }
        for(var i = 0; i < sides.length; ++i){
            if(i+number>3)
            {
                var sideName = sides[i+number-4];
            }
            else{
                var sideName = sides[i+number];
            }
            var sideRoot = game.getChildByName(sideName);
            var BrandwallRoot = sideRoot.getChildByName("Brandwall");
            for(var j = 0; j < BrandwallRoot.children.length; ++j){
                var s = "pai_" + (j+1);
                var n = BrandwallRoot.getChildByName(s);
                // n.active = true;
                this._Brandwall.push(n);            
            }
        }
    },

    //要取得paiDuPos/
    initBrandwalls:function()
    {   
        if(!this.paiqiang)
        {
            this.initpaiqiang();
            this.initcaishen();
        }
        console.log("initBrandwalls");
        var num = cc.vv.gameNetMgr.paiDuPos*2;
        var gangNum=cc.vv.gameNetMgr.getGangs();
        // console.log("gangNum="+gangNum);
        // console.log("cc.vv.gameNetMgr.numOfMJ="+cc.vv.gameNetMgr.numOfMJ);
        // var number=135 - cc.vv.gameNetMgr.numOfMJ + num+6-gangNum
        // console.log("135 - cc.vv.gameNetMgr.numOfMJ + num+6-gangNum="+number);
        // console.log("this._Brandwall.length="+this._Brandwall.length);
        for(var j = num+6; j < 135 - cc.vv.gameNetMgr.numOfMJ + num+6-gangNum; ++j)
        {
            this._Brandwall[j%136].active = false;
        }
        this.index = 134 - cc.vv.gameNetMgr.numOfMJ + num+6 - gangNum;
        this.gangNum = num + 4;

        for(var i = 0;i <gangNum;i++)
            {
                console.log("断线重连........杠"+gangNum);
                this.initGang();
            }
    },

    initcaishen:function(data)
    {
        // console.log("cc.vv.gameNetMgr.paiDuPos" + cc.vv.gameNetMgr.paiDuPos);
        var num = cc.vv.gameNetMgr.paiDuPos*2;
        console.log("num = " + num);
        var sprite =  this._Brandwall[num-2].getComponent(cc.Sprite);
        sprite.node.getChildByName("caishen").active = true;
        this.sprite = sprite;
        var localIndex =cc.vv.gameNetMgr.getLocalIndex(cc.vv.gameNetMgr.button);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        // this.holdSprite = sprite.spriteFrame;
        if(cc.vv.setPeople.isERMJ())
        {
                pre = "B_";
        }
        this.pre = pre;
        sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre,cc.vv.gameNetMgr.caishu);
    },
    initmopai:function()
    {
        this.index++;
        // if(this.index > 135)
        // {
        //     this.index = 0;
        // }
        this._Brandwall[this.index%136].active = false;
    },
    reset:function()
    {
        if(this.sprite == null)
        {
            return;
        }
        var game = this.node.getChildByName("game");
        var localIndex =cc.vv.gameNetMgr.getLocalIndex(this.zhuang);
        var pre = cc.vv.mahjongmgr.getSide(localIndex);
        console.log("pre   " + pre);
         if(cc.vv.setPeople.isERMJ())
        {
                pre = "up";
        }
        this.sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(this.pre);
        // this.sprite.spriteFrame = this.holdSprite;
        // var sides = ["myself","right","up","left"];
        // for(var i = 0; i < sides.length; ++i){
        //     var sideRoot = game.getChildByName(sides[i]);
        //     var BrandwallRoot = sideRoot.getChildByName("Brandwall");
        //     for(var j = 0; j < BrandwallRoot.children.length; ++j){
        //         var s = "pai_" + (j+1);
        //         var n = BrandwallRoot.getChildByName(s);
        //         n.active = true;           
        //     }
        // }
    },
    initpaiqiang:function()
    {
        // console.log("initpaiqiang...........................");
        this.animation.node.active = false;
        var game = this.node.getChildByName("game");
        var sides = ["myself","right","up","left"];
        for(var i = 0; i < sides.length; ++i){
            var sideRoot = game.getChildByName(sides[i]);
            var BrandwallRoot = sideRoot.getChildByName("Brandwall");
            for(var j = 0; j < BrandwallRoot.children.length; ++j){
                var s = "pai_" + (j+1);
                var n = BrandwallRoot.getChildByName(s);
                n.getComponent(cc.Sprite).spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(sides[i]);
                n.active = true;           
            }
        }
        this.paiqiang = true;
    },
    hidepaiqiang:function()
    {
        // console.log("hidepaiqiang...........................");
        var game = this.node.getChildByName("game");
        var sides = ["myself","right","up","left"];
        for(var i = 0; i < sides.length; ++i){
            var sideRoot = game.getChildByName(sides[i]);
            var BrandwallRoot = sideRoot.getChildByName("Brandwall");
            for(var j = 0; j < BrandwallRoot.children.length; ++j){
                var s = "pai_" + (j+1);
                var n = BrandwallRoot.getChildByName(s);
                if((j+1) %2 != 0)
                {
                    n.getChildByName("caishen").active = false;
                }
                n.active = false;           
            }
        }

    },

    initGang:function()
    {
        this._Brandwall[this.gangNum].active = false;
        console.log("gangis"+this.gangNum);
        if(this.gangNum%2 == 0)
        {
            this.gangNum+=1;
        }
        else
        {
            this.gangNum-=3;
        }
        
    },
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
